var express = require('express');
var router = express.Router();
var User = require('../db/user.js');
var article = require('../db/article.js');
var dynamic = require('../db/dynamic.js');
var Collection = require('../db/collections');
const fs = require('fs');


//获取收藏的动态和博文
router.post("/likeAndCollection", function (req, res, next) {
    // console.log(req.body);
    User.findOne({ uid: req.body.uid }, function (err, data) {
        if (err) { console.log(err); return; }
        if (data.likes.length == 0) {
            res.send([]);
        } else {
            async function getLAC() {
                let LCList = [];
                for (let i = 0; i < data.likes.length; i++) {
                    if (data.likes[i].substring(0, 2) == "Ms") {
                        let user = "";
                        await dynamic.findOne({ d_id: data.likes[i] }, function (err, datas) {
                            if (err) { console.log(err); return; }
                            user = datas.uid;
                            LCList.push({
                                type: "Ms",
                                did: datas.d_id,
                                author: datas.uname,
                                showtime: datas.showTime,
                                content: datas.content,
                                picList: datas.pictureUrl,
                                likeNum: datas.likeCount.length,
                                collectionNum: datas.collectionCount.length,
                                commentNum: datas.comment.length,
                            })
                        })
                        await User.findOne({ uid: user }, function (err, USER) {
                            if (err) { console.log(err); return; }
                            LCList[i]["imgHead"] = USER.img_head;
                        })
                    } else {
                        // console.log(2);
                        await article.findOne({ a_id: data.likes[i] }, function (err, datas) {
                            if (err) { console.log(err); return; }
                            LCList.push({
                                type: "As",
                                aid: datas.a_id,
                                author: datas.uname,
                                title: datas.title,
                                description: datas.intr,
                                coverImg: datas.coverImg,
                                viewNum: datas.viewCount,
                                likeNum: datas.likeCount.length,
                                collectionNum: datas.collectionCount.length,
                                commentNum: datas.comment.length,
                            })
                        })
                    }
                }
                return LCList;
            };
            getLAC().then(result => {
                // console.log(result.LCMSList);
                res.send(result);
            });
        }
    })
});


//获取关注的专题列表
router.post("/getAttColls", function (req, res, next) {
    // console.log(req.body.userid);
    User.findOne({ uid: req.body.userid }, function (err, data) {
        if (err) { console.log(err); return; }
        if (data.attentionFile.length > 0) {
            async function getAttColl() {
                let list = [];
                for (let i = 0; i < data.attentionFile.length; i++) {
                    await Collection.findOne({ collectionid: data.attentionFile[i] }, function (err, datas) {
                        if (err) { console.log(err); return; }
                        list.push({
                            cid: datas.collectionid,
                            filename: datas.fileName,
                            fansNum: datas.fansList.length,
                            articlesNum: datas.listAid.length,
                        })
                    })
                }
                return list;
            };
            getAttColl().then(result => {
                // console.log(result);
                res.send(result);
            })
        }
    })
});

//获取关注
router.post("/getfollows", function (req, res, next) {
    User.findOne({ uid: req.body.userid }, function (err, data) {
        if (err) { console.log(err); return; }
        else {
            if (data.follows.length > 0) {
                async function getFollowsList() {
                    let followsList = [];
                    for (let i = 0; i < data.follows.length; i++) {
                        let datafollows = await User.findOne({ uid: data.follows[i] }, function (err, dataf) {
                            if (err) { console.log(err); return; }
                            return dataf;
                        });
                        await article.find({ uid: datafollows.uid, a_type: true }, function (err, a) {
                            if (err) { console.log(err); return; }
                            followsList.push({
                                uid: datafollows.uid,
                                uname: datafollows.uname,
                                imgHead: datafollows.img_head,
                                followsNum: datafollows.follows.length,
                                fansNum: datafollows.fans.length,
                                numOfLikes: datafollows.numOfLikes,
                                AsNum: a.length,
                            })
                        })
                    }
                    return followsList;
                }
                getFollowsList().then(result => {
                    // console.log(result);
                    res.send(result);
                });
            } else {
                res.send("")
            }
        }
    });
});

//获取粉丝
router.post("/getfans", function (req, res, next) {
    User.findOne({ uid: req.body.userid }, function (err, data) {
        if (err) { console.log(err); return; }
        else {
            if (data.fans.length > 0) {
                async function getFansList() {
                    let fansList = [];
                    for (let i = 0; i < data.fans.length; i++) {
                        let datafans = await User.findOne({ uid: data.fans[i] }, function (err, dataf) {
                            if (err) { console.log(err); return; }
                            return dataf;
                        });
                        await article.find({ uid: datafans.uid, a_type: true }, function (err, a) {
                            if (err) { console.log(err); return; }
                            fansList.push({
                                uid: datafans.uid,
                                uname: datafans.uname,
                                imgHead: datafans.img_head,
                                followsNum: datafans.follows.length,
                                fansNum: datafans.fans.length,
                                numOfLikes: datafans.numOfLikes,
                                AsNum: a.length,
                            })
                        })
                    }
                    return fansList;
                }
                getFansList().then(result => {
                    // console.log(result);
                    res.send(result);
                });
            } else {
                res.send("")
            }
        }
    });
});


//获取userbody下的动态
router.post("/userBodyMs", function (req, res, next) {
    dynamic.find({ uid: req.body.userid }, function (err, MsList) {
        if (err) {
            console.log(err);
            return;
        } else {
            User.findOne({ uid: req.body.userid }, function (err, data) {
                if (err) { console.log(err); return; }
                let UserMsList = [];
                for (let i = MsList.length - 1; i >= 0; i--) {
                    if (MsList[i].d_type == true) {
                        if (MsList[i].content.length > 100) {
                            let text = MsList[i].content.substring(0, 99).replace(new RegExp("\n", 'g'), "<br>");;
                            UserMsList.push({
                                did: MsList[i].d_id,
                                imgHead: data.img_head,
                                name: MsList[i].uname,
                                showtime: MsList[i].showTime,
                                content: text,
                                likeNum: MsList[i].likeCount.length,
                                collectionNum: MsList[i].collectionCount.length,
                                picList: MsList[i].pictureUrl,
                                commentNum: MsList[i].comment.length,
                            })
                        } else {
                            let text = MsList[i].content.replace(new RegExp("\n", 'g'), "<br>");
                            UserMsList.push({
                                did: MsList[i].d_id,
                                imgHead: data.img_head,
                                name: MsList[i].uname,
                                showtime: MsList[i].showTime,
                                content: text,
                                likeNum: MsList[i].likeCount.length,
                                collectionNum: MsList[i].collectionCount.length,
                                picList: MsList[i].pictureUrl,
                                commentNum: MsList[i].comment.length,
                            })
                        }

                    }
                };
                res.json({
                    UserMsList: UserMsList,
                })
            });
        }
    });
});

//获取userbody下的文章
router.post("/userBodyAs", function (req, res, next) {
    article.find({ uid: req.body.userid }, function (err, AsList) {
        if (err) {
            console.log(err);
            return;
        } else {
            let UserAsList = [];
            for (let i = AsList.length - 1; i >= 0; i--) {
                if (AsList[i].a_type == true) {
                    UserAsList.push({
                        aid: AsList[i].a_id,
                        name: AsList[i].uname,
                        title: AsList[i].title,
                        intr: AsList[i].intr,
                        coverImg: AsList[i].coverImg,
                        viewNum: AsList[i].viewCount,
                        likeNum: AsList[i].likeCount.length,
                        collectionNum: AsList[i].collectionCount.length,
                        commentNum: AsList[i].comment.length,
                    })
                }
            };
            res.json({
                UserAsList: UserAsList,
            })
        }
    });
});

//获取目标用户主页信息top/side
router.post("/showUser", function (req, res, next) {
    User.findOne({ uname: req.body.username }, function (err, data) {
        if (err) {
            console.log(err);
            return;
        } else {
            // console.log(data);
            article.find({ uid: data.uid, a_type: true }, function (err, AsList) {
                if (err) {
                    console.log(err);
                    return;
                } else {
                    // console.log(AsList);
                    Collection.find({ uid: data.uid }, function (err, CDATA) {
                        if (err) { console.log(err); }
                        let collections = []
                        if (CDATA.length == 0) {

                        } else {
                            for (let i = 0; i < CDATA.length; i++) {
                                collections.push({
                                    cid: CDATA[i].collectionid,
                                    filename: CDATA[i].fileName,
                                })
                            }
                        }
                        res.json({
                            uid: data.uid,
                            uname: data.uname,
                            follows: data.follows.length,
                            fans: data.fans.length,
                            numOfLikes: data.numOfLikes,
                            img_head: data.img_head,
                            uinfor: data.uinfr,
                            article_num: AsList.length,
                            arrangeList: data.arrange,
                            collections: collections,
                        })
                    })
                }
            })
        }
    });
});

//获取自己主页信息top/side
router.post("/showMySelf", function (req, res, next) {
    User.findOne({ uid: req.cookies.userID }, function (err, data) {
        if (err) {
            console.log(err);
            return;
        } else {
            // console.log(data);
            article.find({ uid: req.cookies.userID, a_type: true }, function (err, AsList) {
                if (err) {
                    console.log(err);
                    return;
                } else {
                    // console.log(AsList);
                    Collection.find({ uid: req.cookies.userID }, function (err, CDATA) {
                        if (err) { console.log(err); }
                        let collections = []
                        if (CDATA.length == 0) {

                        } else {
                            for (let i = 0; i < CDATA.length; i++) {
                                collections.push({
                                    cid: CDATA[i].collectionid,
                                    filename: CDATA[i].fileName,
                                })
                            }
                        }
                        res.json({
                            uid: data.uid,
                            uname: data.uname,
                            follows: data.follows.length,
                            fans: data.fans.length,
                            numOfLikes: data.numOfLikes,
                            img_head: data.img_head,
                            uinfor: data.uinfr,
                            article_num: AsList.length,
                            arrangeList: data.arrange,
                            collections: collections,
                        })
                    })
                }
            })
        }
    });
});

module.exports = router